import 'package:flutter/material.dart';
import 'package:zhaoxiaowu_app/components/view.dart';
import 'package:zhaoxiaowu_app/global/global.dart';
import 'package:zhaoxiaowu_app/utils/alert_utils.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_viewmodel.dart';
import 'package:zhaoxiaowu_app/views/accounting/accounting_item.dart';
import 'package:provider/provider.dart';

class AccountingView extends StatefulWidget {
  @override
  _AccountingViewState createState() => _AccountingViewState();
}

class _AccountingViewState extends State<AccountingView> {
  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(
        "记账",
        actions: [
          IconButton(
            icon: Icon(Icons.show_chart),
            onPressed: () => Navigator.of(context).pushNamed("accounting/chart"),
          ),
        ],
        bottom: _getAppBarBottom(),
      ),
      body: ListView.builder(
        shrinkWrap: true, // 根据子组件决定listview的高度
        itemCount: Provider.of<AccountingViewModel>(context).itemCount,
        itemBuilder: _itemBuilder,
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: _add,
      ),
    );
  }

  PreferredSizeWidget _getAppBarBottom() {
    return PreferredSize(
      preferredSize: Size.fromHeight(80),
      child: Container(
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Row(
              children: [
                _getAppBarBottomChild(DateTime.now().year.toString() + "年", fontWeight: FontWeight.bold),
                _getAppBarBottomChild("本月预算", fontWeight: FontWeight.bold),
                _getAppBarBottomChild("收入", fontWeight: FontWeight.bold),
                _getAppBarBottomChild("支出", fontWeight: FontWeight.bold),
              ],
            ),
            SizedBox(height: 8),
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    child: Text(
                      Provider.of<AccountingViewModel>(context).month.toString() + "月",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                      ),
                    ),
                    onTap: _getMonth,
                  ),
                ),
                _getAppBarBottomChild((Global.shared().user?.money ?? 0.0).toString()),
                _getAppBarBottomChild(Provider.of<AccountingViewModel>(context).income.toString()),
                _getAppBarBottomChild(Provider.of<AccountingViewModel>(context).expenditure.toString()),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _getAppBarBottomChild(String text, {FontWeight? fontWeight}) {
    return Expanded(
      child: Text(
        text,
        style: TextStyle(
          color: Colors.white,
          fontSize: 16,
          fontWeight: fontWeight,
        ),
      ),
    );
  }

  Widget _itemBuilder(BuildContext context, int index) {
    return AccountingItem(Provider.of<AccountingViewModel>(context).dataList[index]);
  }

  void loadData() {
    context.read<AccountingViewModel>().getAccountingHistory();
  }

  void _getMonth() async {
    List list = [];
    for (var i = 1; i <= 12; i++) {
      if (i <= DateTime.now().month) list.add(i);
    }

    var result = await showMonthList(list);

    if (result != null) {
      context.read<AccountingViewModel>().setMonth(result);
      loadData();
    }
  }

  void _add() {
    Navigator.of(context).pushNamed("accounting/add");
  }
}
